package com.jlb.demo.entity;

/**
 * 用户实体类 - 演示面向对象的封装、继承特性
 * 继承BaseEntity，获得id、createTime、updateTime字段
 */
public class User extends BaseEntity {
    
    /**
     * 用户名
     */
    private String name;
    
    /**
     * 年龄
     */
    private Integer age;
    
    /**
     * 余额（私有化，通过方法访问 - 封装）
     */
    private Double balance;
    
    /**
     * 是否VIP
     */
    private Boolean isVip;
    
    /**
     * 手机号
     */
    private String phone;
    
    /**
     * 邮箱
     */
    private String email;
    
    /**
     * 默认构造方法
     */
    public User() {
        super();
        this.balance = 0.0;
        this.isVip = false;
    }
    
    /**
     * 带参数的构造方法
     */
    public User(Long id, String name, Integer age) {
        super(id);
        this.name = name;
        this.age = age;
        this.balance = 0.0;
        this.isVip = false;
    }
    
    /**
     * 充值方法 - 演示封装
     * @param amount 充值金额（必须>0）
     * @throws IllegalArgumentException 当金额不合法时抛出异常
     */
    public void recharge(Double amount) {
        if (amount == null || amount <= 0) {
            throw new IllegalArgumentException("充值金额必须大于0");
        }
        this.balance += amount;
        setUpdateTime(); // 更新修改时间
        System.out.println("用户 " + name + " 充值 " + amount + " 元，当前余额：" + balance);
    }
    
    /**
     * 消费方法 - 演示封装
     * @param amount 消费金额（必须>0且≤余额）
     * @throws IllegalArgumentException 当金额不合法时抛出异常
     * @throws IllegalStateException 当余额不足时抛出异常
     */
    public void consume(Double amount) {
        if (amount == null || amount <= 0) {
            throw new IllegalArgumentException("消费金额必须大于0");
        }
        if (amount > this.balance) {
            throw new IllegalStateException("余额不足，当前余额：" + balance + "，需要：" + amount);
        }
        this.balance -= amount;
        setUpdateTime(); // 更新修改时间
        System.out.println("用户 " + name + " 消费 " + amount + " 元，剩余余额：" + balance);
    }
    
    /**
     * 升级VIP - 演示业务逻辑
     */
    public void upgradeToVip() {
        if (this.isVip) {
            System.out.println("用户 " + name + " 已经是VIP用户");
            return;
        }
        this.isVip = true;
        setUpdateTime();
        System.out.println("用户 " + name + " 升级为VIP用户");
    }
    
    /**
     * 判断是否成年
     * @return 是否成年
     */
    public boolean isAdult() {
        return age != null && age >= 18;
    }
    
    /**
     * 获取用户信息摘要
     * @return 用户信息摘要
     */
    public String getSummary() {
        return String.format("用户[ID=%d, 姓名=%s, 年龄=%d, 余额=%.2f, VIP=%s]", 
                getId(), name, age, balance, isVip ? "是" : "否");
    }
    
    // Getter和Setter方法
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Integer getAge() {
        return age;
    }
    
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public Double getBalance() {
        return balance;
    }
    
    public void setBalance(Double balance) {
        this.balance = balance;
    }
    
    public Boolean getIsVip() {
        return isVip;
    }
    
    public void setIsVip(Boolean isVip) {
        this.isVip = isVip;
    }
    
    public String getPhone() {
        return phone;
    }
    
    public void setPhone(String phone) {
        this.phone = phone;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    @Override
    public String toString() {
        return "User{" +
                "id=" + getId() +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", balance=" + balance +
                ", isVip=" + isVip +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                ", createTime=" + getCreateTime() +
                ", updateTime=" + getUpdateTime() +
                '}';
    }
} 